<template>
	<uv-popup ref="privacyAgreementPop" mode="center" round="20" :safeAreaInsetBottom='false'>
		<view class="privacy-container">
			<view class="title">用户隐私保护提示</view>
			<view class="privacy-content">
				<text>感谢您使用本应用，您使用本应用的服务之前请仔细阅读并同意</text>
				<text class="more" @click="openPrivacyContract">《用户隐私保护指引》</text>
				<text>。当您点击同意并开始使用产品服务时，即表示你已理解并同意该条款内容，该条款将对您产生法律约束力。如您拒绝，将无法使用相应服务。</text>
			</view>
			<view class="privacy-footer">
				<button class="is-agree" id="agree-btn" open-type="agreePrivacyAuthorization"
					@agreeprivacyauthorization="handleAgree">
					同意并继续
				</button>
				<button class="is-disagree" id="disagree-btn" @click="handleDisagree">
					不同意
				</button>
			</view>
		</view>
	</uv-popup>
	
</template>


<script>
	const privacyResolves = new Set() // onNeedPrivacyAuthorization的reslove
	let privacyHandler = null
	// 注册监听
	if (uni.onNeedPrivacyAuthorization) {
		uni.onNeedPrivacyAuthorization((resolve) => {
			if (typeof privacyHandler === 'function') {
				privacyHandler(resolve)
			}
		})
	}
	export default {
		data(){
			return {}
		},
		mounted() {
			privacyHandler = (resolve) => {
				this.$refs.privacyAgreementPop.open()
				privacyResolves.add(resolve)
			}
		},
		methods:{
			init(resolve){
				this.$refs.privacyAgreementPop.open()
				this.resolvePrivacyAuthorization = resolve
			},
			// 打开隐私协议
			openPrivacyContract(){
				wx.openPrivacyContract({
				  success: () => {
						console.log('打开成功');
					}, // 打开成功
				  fail: () => {
						uni.showToast({
							title:'打开失败，稍后重试',
							icon: 'none'
						})
					} // 打开失败
				})
			},
			/**
			 * 拒绝隐私协议
			 */
			handleDisagree() {
				this.$refs.privacyAgreementPop.close();
				privacyResolves.forEach((resolve) => {
					resolve({
						event: 'disagree'
					})
				})
				privacyResolves.clear()
				this.$emit('disagree');
			},
			
			/**
			 * 同意隐私协议
			 */
			handleAgree() {
				this.$refs.privacyAgreementPop.close();
				privacyResolves.forEach((resolve) => {
					resolve({
						event: 'agree',
						buttonId: 'agree-btn'
					})
				})
				privacyResolves.clear()
				this.$emit('agree')
			}
		}
	}
</script>

<style lang="scss" scoped>
	.privacy-container {
		width: 600rpx;
		padding: 38rpx;
		box-sizing: border-box;
		overflow: hidden;
		width: 560rpx;
		background: linear-gradient(180deg, #e5edff 0%, #ffffff 100%);
		.title {
			display: flex;
			align-items: center;
			justify-content: center;
			width: 100%;
			height: 52rpx;
			font-size: 36rpx;
			font-weight: 550;
			color: #1a1a1a;
			line-height: 52rpx;
			margin-bottom: 48rpx;
		}
		
		.privacy-content {
			width: 100%;
			box-sizing: border-box;
			font-size: 28rpx;
			font-family: PingFangSC-Regular, PingFang SC;
			font-weight: 400;
			color: #333333;
			line-height: 48rpx;
			margin-bottom: 48rpx;
			.more {
				color: #39b54a;
				font-weight: 600;
			}
		}
		
		.privacy-footer {
			display: flex;
			flex-direction: column;
			.is-disagree,
			.is-agree {
				width: 100%;
				height: 88rpx;
				background: #ffffff;
				border-radius: 44rpx;
				font-size: 32rpx;
				font-family: PingFangSC-Regular, PingFang SC;
				font-weight: 400;
				color: #666666;
			}
		
			.is-agree {
				background: #39b54a;
				color: #ffffff;
				margin-bottom: 18rpx;
			}
			button {
				border: none;
				outline: none;
				&::after {
					border: none;
				}
			}
		}
	}
</style>
